Partial Class Admin_Add_RiskProfile_Header
    Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 1, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing
    End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If DataGrid.Items.Count < 1 Then
      Dim zClass As New LookUp.General_Lookup
      zClass.LookUpCode = "RISK"
      DataGrid.CurrentPageIndex = 0
      zClass.DataGridFill(DataGrid, Results_lbl)

    End If

    Dim Edit_Image As Image = CType(Me.Master.FindControl("L20"), Image)
    Edit_Image.ImageUrl = "~/images/redT.PNG"
    Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("RiskProfile_btn"), LinkButton)
    Edit_Button.ForeColor = Drawing.Color.Black
    Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Lookup_Panel"), Panel)
    Edit_Panel.Visible = True
        SecurePage()
  End Sub

  Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
    OnMode("Add") : ViewState("mode") = "Add"
    Heading_txt.Text = ""
    Heading_txt.Focus()
  End Sub

  Private Sub OnMode(ByVal mode As String)

    If mode = "Edit" Then
      'no code
    End If

    DataGrid.Enabled = False
    Add_btn.Enabled = False
    AddEdit_Panel.Visible = True

  End Sub

  Private Sub NotOnMode()
    DataGrid.Enabled = True
    Add_btn.Enabled = True
    AddEdit_Panel.Visible = False

  End Sub

  Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
    NotOnMode()
    ViewState.Remove("mode")
  End Sub

  Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO

        If ViewState("mode") = "Add" Then
            zClass.LookUpCode = "RISK_PROFILE"
            zClass.ExecQuery("INSERT INTO risk_profile_mstr(risk_profile_cd, risk_profile_desc, " & _
                "risk_profile_crea_by, risk_profile_crea_dt) VALUES('" & _
                Trim(zClass.MaxCode()) & "'," & UCase(dm.Q(Trim(Heading_txt.Text))) & _
                ",'" & UCase(Session("UserName")) & "','" & Now & "')")

        ElseIf ViewState("mode") = "Edit" Then
            zClass.ExecQuery("UPDATE risk_profile_mstr SET " & _
                "risk_profile_desc=" & UCase(dm.Q(Trim(Heading_txt.Text))) & ", " & _
                "risk_profile_last_upd_by='" & Session("UserName") & "', " & _
                "risk_profile_last_upd_dt='" & Now & "' " & _
                "WHERE risk_profile_cd='" & Trim(ViewState("risk_cd").ToString) & "'")

        End If

        zClass.LookUpCode = "RISK"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl)
        NotOnMode()
        ViewState.Remove("mode")
        ViewState.Remove("risk_cd")
  End Sub

  Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
    If e.CommandName = "Delete" Then
      Dim dm As New DAOMain.CommonDAO
      Dim zClass As New LookUp.General_Lookup
      zClass.LookUpCode = "RISK"

            If zClass.OKtoDELETE(Trim(e.Item.Cells(0).Text)) = False Then
                Dim pFunc As New PolyFunc.ASPFunc

                pFunc.MessageAlert(Me, "This item still contains details. Delete failed!")
                Exit Sub
            End If


      zClass.ExecQuery("DELETE FROM risk_profile_mstr WHERE risk_profile_cd=" & dm.Q(Trim(e.Item.Cells(0).Text)))

      DataGrid.CurrentPageIndex = 0
      zClass.DataGridFill(DataGrid, Results_lbl)

    ElseIf e.CommandName = "Edit" Then
      ViewState("mode") = "Edit"
      Heading_txt.Text = e.Item.Cells(1).Text
      ViewState("risk_cd") = e.Item.Cells(0).Text
      OnMode("Edit")
      Heading_txt.Focus()

    ElseIf e.CommandName = "Details" Then
      Session("risk_profile_cd") = e.Item.Cells(0).Text
      Session("risk_profile_desc") = e.Item.Cells(1).Text
      Response.Redirect("~/Admin/Add_RiskProfile_Detail.aspx")
    End If

  End Sub

  Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
    DataGrid.CurrentPageIndex = e.NewPageIndex
    Dim zClass As New LookUp.General_Lookup
    zClass.LookUpCode = "RISK"
    zClass.DataGridFill(DataGrid, Results_lbl)

  End Sub


End Class